﻿namespace SandScript.Wrappers
{
    using System.Diagnostics;
    using Models;

    public class ProcessHelper : IProcessHelper
    {
        public ProcessResult RunExecutableAndCaptureOutput(string exePath, string arguments)
        {
            string standardOutput;
            int exitCode;

            using (var process = new Process())
            {
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.FileName = exePath;
                process.StartInfo.Arguments = arguments;
                process.Start();

                standardOutput = process.StandardOutput.ReadToEnd();

                process.WaitForExit();

                exitCode = process.ExitCode;
            }

            return new ProcessResult(exitCode, standardOutput);
        }
    }
}
